<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle">
    <el-form
      :model="formData"
      ref="formRef"
      :rules="formRules"
      vloading="formLoading"
      label-width="auto"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="客户编号" prop="customerNo">
            <el-input v-model="formData.customer.customerNo" disabled class="mr-1" />
            <el-button @click="clickChooseCustomer" v-if="!isEdit">选择老人</el-button>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="客户名称" prop="customerName">
            <el-input v-model="formData.customer.name" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="订单总金额" prop="totalAmount">
            <InputNumber
              v-model="formData.totalAmount"
              type="decimal"
              :min="0"
              placeholder="请输入订单总金额"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="实付金额" prop="payAmount">
            <InputNumber
              v-model="formData.payAmount"
              type="decimal"
              :min="0"
              placeholder="请输入实付金额"
              :clearable="true"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button type="primary" :disabled="formLoading" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
  <CustomerSelect ref="selectRef" @row-dblclick="handleSelectCustomerInfo" />
</template>
<script setup name="salesOrderEdit" lang="ts">
import * as SalesApi from '@/api/order/sales'
import { InputNumber } from '@/components/InputNumber'
import CustomerSelect from '@/views/customer/customer/CustomerSelect.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const selectRef = ref()
const dialogTitle = ref('')
const dialogVisible = ref(false)
const formRef = ref()
const formLoading = ref(false)
const isEdit = ref(false)
const formData = ref({
  id: undefined,
  customer: { customerNo: '', name: '' },
  customerId: undefined,
  totalAmount: 0,
  payAmount: 0
})
const formRules = reactive({})
/** 打开弹窗 */
const open = async (id, type) => {
  resetData()

  if (type == 'update') {
    formData.value = await SalesApi.singleGet(id)
    isEdit.value = true
  }
  dialogVisible.value = true
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const resetData = () => {
  formData.value = {
    id: undefined,
    customer: { customerNo: '', name: '' },
    customerId: undefined,
    totalAmount: 0,
    payAmount: 0
  }
  isEdit.value = false
}

const clickChooseCustomer = () => {
  selectRef.value.open()
}

const handleSelectCustomerInfo = (selectCustomerInfo) => {
  formData.value.customer.name = selectCustomerInfo.name
  formData.value.customer.customerNo = selectCustomerInfo.customerNo
  formData.value.customerId = selectCustomerInfo.id
}

const emit = defineEmits(['success'])
/** 提交表单 */
const submitForm = async () => {
  try {
    if (formData.value.id == undefined) {
      await SalesApi.create(formData.value)
      message.success(t('common.createSuccess'))
    } else {
      await SalesApi.update(formData.value)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    emit('success')
  } finally {
  }
}
</script>
